<?php
/**
 * Joomla! 1.5 component JoomGPSTracks
 *
 * @version $Id: view.html.php 2009-06-24 15:40:19 svn $
 * @author Michael Pfister
 * @package joomGPStracks
 * @subpackage frontend
 * @license GNU/GPL
 * @filesource
 *
 * 
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view');
jimport('joomla.html.pagination');

/**
 * HTML View class for the JoomGPSTracks component
 */
class JoomgpstracksViewTracks extends JView {
    /**
     *
     * @param <type> $tpl
     * @return <type>
     */
	function display($tpl = null) {

        if($this->getLayout() == 'form'):
			$this->_displayForm($tpl);
            return;
		endif;

        if($this->getLayout() == 'track'):
            $this->_displayTrack($tpl);
            return;
        endif;

        if($this->getLayout() == 'list'):
            $this->_displayList($tpl);
            return;
        endif;

        if($this->getLayout() == 'user'):
            $this->displayUserTracks($tpl);
            return;
        endif;
        
        parent::display($tpl);
    }

    /**
     *
     * @global <type> $mainframe
     * @param <type> $tpl
     * @return <type>
     */
    function _displayForm($tpl = null)  {
        global $mainframe;

        JHTML::script('jquery.js', 'components/com_joomgpstracks/assets/js/', false);
        JHTML::script('multifile.js', 'components/com_joomgpstracks/assets/js/', false);
        JHTML::_('behavior.modal');
        JHTML::_('behavior.tooltip');
        $cache =& JFactory::getCache('com_joomgpstracks');
        
        $cfg = joomgpstracksHelper::getConfig();
        
	// Make sure you are logged in and have the necessary access rights
        $user =& JFactory::getUser();
        if ($user->get('gid') < $cfg->gid) {
            JResponse::setHeader('HTTP/1.0 403',true);
            JError::raiseWarning( 403, JText::_('ALERTNOTAUTH') );
            return;
        }

        $editor =& JFactory::getEditor();
        $pathway =& $mainframe->getPathway();
        $lh = layoutHelper::navigation();
        $footer = layoutHelper::footer();
        $model = $this->getModel();
        $uri =& JFactory::getURI();
        // Add pathway item
        $pathway->addItem(JText::_('New'), '');
        $mainframe->setPagetitle(JText::_('GPS_NEW_TRACK'));
        $row = $model->getCats();
//        $terrain = $model->getTerrain();
        $terrain = $cache->get(array($model, 'getTerrain'), array());
        $terms = JRoute::_('index2.php?option=com_content&view=article&id='.$cfg->terms_id);
        $id =(JRequest::getInt('id', NULL));
        $catid = "";
        // update part
        if(isset($id))  {
//            $track = $model->getTrack($id);
            $track = $cache->get(array($model, 'getTrack'), array($id));
            $this->assignRef('track',   $track);
            $this->assignRef('id',  $id);
            $catid = $track->catid;
            $pathway->addItem(JText::_('GPS_UPDATE'), '');
            $mainframe->setPagetitle(JText::_('GPS_UPDATE'));
            $selterrain = explode(',', $track->terrain);
        }
        $access = array(
            array('id' => 0, 'text' => JText::_('GPS_PUBLIC')),
            array('id' => 1, 'text' => JText::_('GPS_REGISTERED'))
        );

        $lists['content'] = JHTML::_('select.genericlist',   $row, 'catid', 'size="1"', 'id', 'title', $catid );
        $lists['terrain'] = JHTML::_('select.genericlist',   $terrain, 'terrain[]', 'multiple="multiple" size="5"', 'title', 'title', $selterrain );
        $lists['access'] = JHTML::_('select.genericlist',   $access, 'access', 'size="2"', 'id', 'text', $track->access );

        $this->assignRef('kml',         $start);
        $this->assignRef('lh',          $lh);
        $this->assignRef('footer',      $footer);
        $this->assignRef('editor',      $editor);
        $this->assignRef('lists',       $lists);
        $this->assignRef('action',      $uri->toString());
        $this->assignRef('cfg',         $cfg);
        $this->assignRef('terms',       $terms);
        $this->assignRef('comments',    $comments);

        parent::display($tpl);
    }

    function _displayTrack($tpl)  {
        global $mainframe;

        JHTML::_('behavior.modal');
        JHTML::_('behavior.combobox');
        $cache =& JFactory::getCache('com_joomgpstracks');
        
        $lh = layoutHelper::navigation();
        $footer = layoutHelper::footer();
        $cfg = joomgpstracksHelper::getConfig();
        $model = $this->getModel();
        $pathway =& $mainframe->getPathway();
        $id =& JRequest::getInt('id');
//        $track = $model->getTrack($id);
        $track = $cache->get(array($model, 'getTrack'), array($id));
//        $vote = $model->getVotes($id);
        $vote = $cache->get(array($model, 'getVotes'), array($id));
        $pathway->addItem($track->title, '');
        $mainframe->setPageTitle($track->title);
        $date = JHTML::_('date', $track->date, JText::_('DATE_FORMAT_LC4'));
        $profile = getProfileLink($track->uid, $track->user);
//        $comments = $model->getComments($id, $cfg->ordering);
        $comments = $cache->get(array($model, 'getComments'), array($id, $cfg->ordering));
        $user =& JFactory::getUser();
        
        
        $document =& JFactory::getDocument();
        $document->addScript('http://www.google.com/jsapi?key='.$cfg->apikey);
        $document->addScript('http://www.google.com/uds/api?file=uds.js&v=1.0&key='.$cfg->apikey);
        $document->addScript('http://ajax.googleapis.com/ajax/libs/dojo/1.3.2/dojo/dojo.xd.js.uncompressed.js');
        $document->addStylesheet('http://www.google.com/uds/css/gsearch.css');
        JHTML::script('joomGPStracks.js', 'components/com_joomgpstracks/assets/js/', false);
        JHTML::script('jd.gallery.js', 'components/com_joomgpstracks/assets/js/', true);
        
        $action = "index.php?option=com_joomgpstracks&controller=download&task=download";

        $gps = new gpsClass();
//        $map = $gps->writeTrack($track);
        $map = $cache->get(array($gps, 'writeTrack'), array($track));
        $unit = $cfg->unit;
        $distance = $track->distance;
        $disunit = "Km";
        if($unit == "Miles"):
            $distance = getMiles($track->distance);
            $disunit = "Mi";
        endif;
        $levelimg = $model->createLevelImg($track->level);
        // charts
        $file = './components/com_joomgpstracks/uploads/'.strtolower($track->file);
        print_r($positions);
//        $coords = $gps->getCoords($file);
        $coords = $cache->get(array($gps, 'getCoords'), array($file));
        if($coords[0][2] > 0)  {
//        $chartdata = $gps->createChartsData($coords);
        $chartdata = $cache->get(array($gps, 'createChartsData'), array($coords));
//        $cdis = $gps->distance2chart($distance);
        $cdis = $cache->get(array($gps, 'distance2chart'), array($distance));
        }
        // heartbeat
        if(isset($coords[0][4]) && $coords[0][4] > 0)  {
//            $beat = $gps->createBeatsData($coords);
            $beat = $cache->get(array($gps, 'createBeatsData'), array($coords));
            $this->assignRef('beat',   $beat);
        }
        
        // load images if exists
        jimport('joomla.filesystem.file');
        if(JFolder::exists(JPATH_SITE.DS.'images'.DS.'joomgpstracks'.DS.md5($track->title))) {
            $exclude = array('.db','.txt');
            $images = JFolder::files(JPATH_SITE.DS.'images'.DS.'joomgpstracks'.DS.md5($track->title), '', true, false, $exclude);
        }

        $this->assignRef('lh',          $lh);
        $this->assignRef('footer',      $footer);
        $this->assignRef('map',         $map);
        $this->assignRef('cfg',         $cfg);
        $this->assignRef('track',       $track);
        $this->assignRef('vote',        $vote);
        $this->assignRef('distance',    $distance);
        $this->assignRef('action',      $action);
        $this->assignRef('images',      $images);
        $this->assignRef('date',        $date);
        $this->assignRef('profile',     $profile);
        $this->assignRef('unit',        $disunit);
        $this->assignRef('chart',       $chartdata);
        $this->assignRef('comments',    $comments);
        $this->assignRef('user',        $user);
        $this->assignRef('model',       $model);
        $this->assignRef('levelimg',    $levelimg);


        parent::display($tpl);
    }

    function _displayList($tpl)  {
        global $mainframe, $option;

        $model =& $this->getModel();
        $cache =& JFactory::getCache('com_joomgpstracks');

        $lh = layoutHelper::navigation();
        $footer = layoutHelper::footer();
        $cfg = joomgpstracksHelper::getConfig();
        $pathway =& $mainframe->getPathway();
        $pathway->addItem(JText::_('GPS_TRACKS'), '');
        $mainframe->setPageTitle(JText::_('GPS_TRACKS'));
        $params	= &$mainframe->getParams();

        $order = JRequest::getVar( 'order', 'order', 'post', 'string' );

        $filter_order		= $mainframe->getUserStateFromRequest( "$option.filter_order",		'filter_order',		'ordering',	'cmd' );
        $filter_order_Dir	= $mainframe->getUserStateFromRequest( "$option.filter_order_Dir",	'filter_order_Dir',	'',			'word' );
        $search			= $mainframe->getUserStateFromRequest( "$option.search",			'search',			'',		'string' );
	$search			= JString::strtolower( $search );
        $limit                  = $mainframe->getUserStateFromRequest( $option.'.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
	$limitstart             = $mainframe->getUserStateFromRequest( $option.'.limitstart', 'limitstart', 0, 'int' );
        $limitstart = JRequest::getVar('limitstart',0,'','int');
        $action = JRoute::_('index.php?option=com_joomgpstracks&view=tracks&layout=list');

        $lists['order']		= $filter_order;
        $lists['order_Dir']	= $filter_order_Dir;
        $lists['search']= $search;

//	$rows =& $model->getData($limit, $limitstart );
        $rows = $cache->get(array($model, 'getData'), array($limit, $limitstart));
	$total		= & $this->get( 'Total');
        $pagination =& new JPagination($total, $limitstart, $limit);

        $this->assignRef('lists',       $lists);
        $this->assignRef( 'rows',       $rows);
        $this->assignRef('pagination',	$pagination);
        $this->assignRef('lh',          $lh);
        $this->assignRef('footer',      $footer);
        $this->assignRef('action',      $action);
        $this->assignRef('cfg',         $cfg);
        $this->assignREf('params',      $params);
        
        parent::display($tpl);
    }

    function displayUserTracks($tpl)  {
        global $mainframe, $option;

        $lh = layoutHelper::navigation();
        $footer = layoutHelper::footer();
        $model =& $this->getModel();
        $cfg = joomgpstracksHelper::getConfig();
        $pathway =& $mainframe->getPathway();
        $pathway->addItem(JText::_('GPS_MY_TRACKS'), '');
        $mainframe->setPageTitle(JText::_('GPS_MY_TRACKS'));

        $order = JRequest::getVar( 'order', 'order', 'post', 'string' );

        $filter_order		= $mainframe->getUserStateFromRequest( "$option.filter_order",		'filter_order',		'ordering',	'cmd' );
        $filter_order_Dir	= $mainframe->getUserStateFromRequest( "$option.filter_order_Dir",	'filter_order_Dir',	'',			'word' );
	$search                 = $mainframe->getUserStateFromRequest( "$option.search",			'search',			'',		'string' );
	$search			= JString::strtolower( $search );
        $limit                  = $mainframe->getUserStateFromRequest( $option.'.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
	$limitstart             = $mainframe->getUserStateFromRequest( $option.'.limitstart', 'limitstart', 0, 'int' );
        $limitstart = JRequest::getVar('limitstart',0,'','int');
        $action = JRoute::_('index.php?option=com_joomgpstracks&view=tracks&layout=user');

        $lists['order']		= $filter_order;
        $lists['order_Dir']	= $filter_order_Dir;
        $lists['search']        = $search;

        $rows =& $model->getData($limit, $limitstart );
	$total = & $this->get( 'Total');
        $pagination =& new JPagination($total, $limitstart, $limit);

        $this->assignRef('lh',          $lh);
        $this->assignRef('footer',      $footer);
        $this->assignRef('action',      $action);
        $this->assignRef('cfg',         $cfg);
        $this->assignRef('lists',       $lists);
        $this->assignRef( 'rows',       $rows);
        $this->assignRef('pagination',	$pagination);

        parent::display($tpl);
    }
}

?>